<template lang="jade">

div.user-center

	div.survey_show
		dialog(:show.sync="show_dialog")
			a(@click="goSurvey()")
				img.cover(src="../assets/images/user/survey_dm.png")
			span(@click="setdialog")
				i.iconfont &#xe629;

	div.auto_buy
		dialog(:show.sync="auto_buy_dialog")
			div.autobuy
				b 请输入手机验证码：
				div(style="vertical-align:middle")
					input(type="text", v-model="returnSMS.OTPNO", style="vertical-align:middle")
					button( style="vertical-align:middle",  @click="sentSMS")
						{{ msn.btnTxt }}
				div
					button(@click="stopAutoBuy") 提交
			i.iconfont(@click="close_auto") &#xe629;

	header
		i.iconfont.setting(@click="goSetUp()") &#xe62a;
		div.top-content
			div.protrait(style="background:url({{info.avatar}});background-size:cover;border-radius:50%")
				span(v-bind:class=" pinggu ? 'survey_icon done':'survey_icon'  ", @click="goSurvey()")
			div.value
				b 您好 {{info.phone}}
				i {{info.pinggu_str}}
				strong {{info.total}}
				em 总资产(元)
			ul.cbox.nav
				li
					b {{info.earnings}}
					span 昨天收益(元)
				li
					b {{info.profit}}
					span 累计总收益(元)
	div.btnlist
		a.p_btn_nob(v-link="{ path: '/withdraw-cash'}") 提现
		a.p_btn(v-link="{ path: '/recharge'}") 充值

	ul.moneylist.clearfix
		li
			strong 可用余额(元)
			span {{info.balance}}
		li
			strong 待收总额(元)
			span {{info.total_collect}}
		li
			strong 待收本金(元)
			span {{info.total_capital}}
		li
			strong 待收利息(元)
			span {{info.total_interest}}
		li
			strong 本月待收(元)
			span {{info.recover_account_wait_month}}
		li
			strong 本月已收(元)
			span {{info.recover_account_yes_month}}
		li
			strong 推荐收益(元)
			span {{info.rec_reward}}
		li
			strong 冻结资金(元)
			span {{info.frost}}

	ul.s-menu.bottom(style="margin-bottom: 2.5rem;")



		li.sp
			a.txt(v-link="{ path: '/user/sign-contract'}")
				i.iconfont.ft &#xe633;
				电子签章及授权
			i.iconfont.se &#xe600;

		li.sp
			a.txt(v-link="{ path: '/auto-buy'}")
				i.iconfont.ft &#xe633;
				自动投标
			span.disa
				input(type="checkbox", class="chk_3",  id="checkbox1", v-bind:checked = " opened ")
				label(for="checkbox1", @click="setAuto()")
			i.iconfont.se &#xe600;


		li(v-link="{ path: '/invest-record'}")
			i.iconfont.ft &#xe62c;
			投资明细

			i.iconfont.se &#xe600;

		li(v-link="{ path: '/money-record'}")
			i.iconfont.ft &#xe636;
			交易流水
			i.iconfont.se &#xe600;

		li(v-show="info.checkIntegral", v-link="{ path: '/shopping'}")
			i.iconfont.ft &#xe63b;
			我的积分
			i.iconfont.se &#xe600;


		li(v-link="{ path: '/withdraw-record'}")
			i.iconfont.ft &#xe63c;
			提现记录
			i.iconfont.se &#xe600;

		li(v-link="{ path: '/recharge-record'}")
			i.iconfont.ft &#xe63d;
			充值记录
			i.iconfont.se &#xe600;

		li(v-link="{ path: '/friends-report'}")
			i.iconfont.ft &#xe63a;
			我的推荐奖励
			i.iconfont.se &#xe600;

		li(v-link="{ path: '/promotion/170711'}")
			i.iconfont.ft &#xe63b;
			推荐有礼
			i.iconfont.se &#xe600;

		li(v-link="{ path: '/message'}")
			i.iconfont.ft &#xe630;
			站内信息
			i.iconfont.se &#xe600;

		li(v-link="{ path: '/user/profile'}")
			i.iconfont.ft &#xe630;
			个人设置
			i.iconfont.se &#xe600;

formpage(:url.sync="post_url", :rdata.sync="post_data")
</template>


<style>
@import '~vux/dist/vux.css';
</style>


<script>
var time_work = null, vm = this;
import {Api_Set_Auto, c_user,Api_Send_Auto_SMS, Api_Send_Set_Auto_Buy} from '../config/api'
import {Dialog, Switch} from 'vux'
import FormPage from '../components/common/form'

import api from '../data/api';

import '../assets/sass/user/auto-buy.scss'

export default {

	data () {
		return {
			post_url: '',
			post_data:{},
			show_dialog:false,
			busy:false,
			info:{},
			auto_buy_dialog:false,
			pinggu:true,
			opened:false,
			schecked:'',
			subForm:null,

			msn:{
				stop:false,
				disableBtn:false,
				btnTxt:"重新发送",
				timer:0
			},

			returnSMS:{
				OTPSEQNO: '',
				OTPNO: '',
				status: false
			}

		}
	},
	components: {
		formpage:FormPage,
		dialog:Dialog,
		switch:Switch
	},
	methods: {
		getForm(){
			for(var a in this.$children){
				if(this.$children[a].name == 'form')
					this.subForm = this.$children[a];
			}
		},

		showSurvey(){
			this.show_dialog = true;
		},

		setAuto(){
			if(this.opened == false){

				this.$store.dispatch('setShowLoading', true);
				this.busy = true;
				var data = {};
				this.$http.post(Api_Set_Auto, data,'').then(function (response) {
					var rs = response.data;
					this.$store.dispatch('setShowLoading', false);

					if(!rs.status){
						alert(rs.msg)
						this.busy = false;
					}else {
						/*this.$children[1].show = true
						this.post_url = rs.action_url
						this.post_data.RequestData = rs.RequestData
						this.post_data.transCode = rs.transCode
						this.$children[1].submit();*/
									this.getForm();
									this.subForm.show = true
									this.post_url = rs.action_url
									this.post_data.RequestData = rs.RequestData
									this.post_data.transCode = rs.transCode
									this.subForm.submit();
					}
				}, function (response) {

				});

			}
			else{
				this.msn.disableBtn = true, this.startTimer(), this.auto_buy_dialog = true;

				if(this.msn.timer > 0 && this.msn.timer < 60 ) return;

				this.sendAutoConfirmSMS();

			}

		},

		sendAutoConfirmSMS(){
			var data = {};
			if(this.busy){
				return;
			}
			this.busy = true;
			//this.$store.dispatch('setShowLoading', true);
			this.$http.post(Api_Send_Auto_SMS, data,'').then(function (response) {
				var obj = response.data;
				//this.$store.dispatch('setShowLoading', false);
				this.busy = false;
				if(!obj.status){
					alert(obj.msg);
					this.stopTimeCount();
				}else{
					this.returnSMS.OTPSEQNO = obj.OTPSEQNO;
					this.returnSMS.status = obj.status;
				}
			});
		},

		//关闭自动投标
		stopAutoBuy(){
			//if(this.returnSMS.status == false || this.busy){
			if(this.returnSMS.status == false || this.busy == true || this.returnSMS.OTPNO =='' ){
				return;
			}else{
				this.busy = true, this.$store.dispatch('setShowLoading', true);
				var data = {
					OTPSEQNO: this.returnSMS.OTPSEQNO,
					OTPNO: this.returnSMS.OTPNO
				};
				this.$http.post(Api_Send_Set_Auto_Buy, data,'').then(function (response) {
					var obj = response.data;
					this.$store.dispatch('setShowLoading', false);
					alert(obj.msg);
					this.busy = false;
					//this.stopTimeCount();

					if(obj.status){
						this.auto_buy_dialog = false
						this.opened  = false;
					}

				});

			}

		},

	    updateSms () {

	    	console.log('get ')
	        if(this.msn.timer <= 0)
	        {
				this.stopTimeCount();
	        }
	        else{
	            this.msn.timer--;
	            this.msn.btnTxt = '剩余'+this.msn.timer+'秒';
	        }

	    },

		stopTimeCount(){
			clearInterval(time_work);
			this.msn.timer  = 0;
			this.msn.stop = false;
			this.msn.disableBtn = false;
			this.msn.btnTxt = "重新发送";
		},
	    startTimer () {
			clearInterval(time_work);
			//if(this.msn.timer>0) return;
			if(this.msn.timer == 0)	this.msn.timer = 60;
	        if(this.msn.stop == false)
	        {
				time_work = setInterval(this.updateSms,1000);
	        }
	        else
	        {
				clearInterval(time_work);
	        }

	        //this.msn.stop = !this.msn.stop;
	    },
		sentSMS(){
			if(this.msn.disableBtn){
				return;
			}
			console.log('sentSMS');
			this.msn.disableBtn = true, this.startTimer();
			this.sendAutoConfirmSMS();
		},
/*  	switchAuto(key){
			//console.log(this.opened)
			if(this.opened == false)
				window.location.href = Api_Set_Auto;
			else{
				this.auto_buy_dialog = true;
			}
		},*/
		goSurvey(){
			this.$route.router.go('/survey')
		},

		close_auto(){
			this.auto_buy_dialog = !this.auto_buy_dialog;
		},

		setdialog(){
			this.show_dialog = !this.show_dialog;
		},

		async fetch() {
			vm = this;
			vm.$store.dispatch('setShowLoading', true);

			/* this.$http({url: c_user, method: 'GET', timeout:10000}).then(function (response) {


				var obj = response.data;
				this.$store.dispatch('setShowLoading', false);
				if(obj.code == '10055'){
					sessionStorage.removeItem("login");
					that.$route.router.go('/login?from=user');
					return;
				}

				that.info = obj.data
				if( obj.status == true )
					that.invit_status = that.info.invit_status

				if(that.info.pinggu==1){
					that.show_dialog = true;
					that.pinggu = false;
				}

				if(that.info.open == 1){
					this.opened = true;
				}
				else{
					this.opened = false;
				}


				}, function (res) {

					if(res.status==408 || res.ok==false){
						this.fetch();
					}else{
						console.log(res)
					}
			}); */



			var res = await api.user.info();
			vm.info = res.data;
			vm.$store.dispatch('setShowLoading', false);

			if(res.code == '10055'){
				sessionStorage.removeItem("login");
				vm.$route.router.go('/login?from=user');
				return;
			}

			if( res.status == true )
				vm.invit_status = vm.info.invit_status

			if(vm.info.pinggu==1){
				vm.show_dialog = true;
				vm.pinggu = false;
			}

			if(vm.info.open == 1)
				vm.opened = true;
			else
				vm.opened = false;

		},

		setMenu: function (next) {
			this.showMenu = !this.showMenu
		},

		goSetUp: function (next) {
			this.$route.router.go('/user-setup?tel='+this.info.phone+'&name='+this.info.bank_AcName+'&acno='+this.info.bank_AcNo+'&avatar='+this.info.avatar)
			//this.$route.router.go('/user-setup');
		}

	},

	route: {
		activate ( transition ) {
			//console.log(sessionStorage.getItem("loan"));
			//this.header = true,
			//this.tabbar = true;
			vm = this;
			document.title = "用户中心 - 有喜财富手机版"
			transition.next();
			this.fetch()

		}
	}
}
</script>
